public class Text {
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if(k == 0 || arr == null) {
            return ret;
        }
        PriorityQueue<Integer> priority = new PriorityQueue<>(new Comparator<Integer>() {
            public int compare(Integer o1,Integer o2) {
                return o2.compareTo(o1);
            }
        });
        for(int i = 0;i < k; i++) {
            priority.offer(arr[i]);
        }
        for(int i = k;i<arr.length;i++){
            int val = priority.peek();
            if(val > arr[i]) {
                priority.poll();
                priority.offer(arr[i]);
            }
        }
        for(int i = 0;i<k;i++) {
            ret[i] = priority.poll();
        }
        return ret;
    }
}
